﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace HobSoft.SoccerBet2014.Domain.Common.Site
{
    using SiteContext;
    using Domain.Models;
    public partial class IbcSessionManager : SessionManagerBase
    {
        internal protected class Tournament
        {
            public Tournament(int id, string name)
            {
                ID = id;
                Name = name;
            }
            private int _id;
            public int ID
            {
                get { return _id; }
                set { _id = value; }
            }

            private string _name;
            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }

        }
        internal protected class OddsDataFormParamters
        {
            public OddsDataFormParamters()
            {
                Sport = 1;
                Game = 0;
                OrderBy = 0;
                OddsType = 4;
                MainLeague = 1;
                RT = "W";
                Key = "dodds";
                //DT = DateTime.Now.ToString("M/d/yyyy");
            }
            public string Market { get; set; }
            public int Sport { get; set; }
            /// <summary>
            /// 结束时间
            /// </summary>
            public string DT { get; set; }
            /// <summary>
            /// 请求类型W: all, U: update
            /// </summary>
            public string RT { get; set; }
            /// <summary>
            /// 开始时间
            /// </summary>
            public string CT { get; set; }
            public int Game { get; set; }
            /// <summary>
            /// 0-普通排序 1- 时间排序
            /// </summary>
            public int OrderBy { get; set; }
            public int OddsType { get; set; }
            public int MainLeague { get; set; }
            public string Key { get; set; }
            public string ExtKey { get; set; }
            public string ExtValue { get; set; }
            public string DispRang { get; set; }
            public override string ToString()
            {
                return string.Format("Market={0}&Sport={1}&DT={2}&RT={3}&CT={4}&Game={5}&OrderBy={6}&OddsType={7}&MainLeague={8}&{9}={10}&key={11}"
                    , Market, Sport, DT, RT, CT, Game, OrderBy, OddsType, MainLeague, ExtKey, ExtValue, Key);
            }
        }

        protected readonly Dictionary<string, int> GameEventAppendMapping = new Dictionary<string, int>()
        {
            {"MUID",  0},
            {"RedCardH",  1},
            {"RedCardA",  2}
        };

        protected readonly Dictionary<string, int> GameEventBaseInfoMapping = new Dictionary<string, int>()
        {
            {"MUID",  0},
            {"ShowTime",  1},
            {"StatusId",  2},
            {"SportRadar",  3},
            {"StreamingId",  4},
            {"Favorite",  5},
            {"HomeName",  6},
            {"AwayName",  7},
            {"KickoffTime",  8}
        };

        protected readonly Dictionary<string, int> GameEventLiveStateMapping = new Dictionary<string, int>()
        {
            {"MUID",  0},
            {"ShowTime",  1},
            {"CsStatus",  2},
            {"InjuryTime",  3},
            {"LivePeriod",  4},
            {"ScoreH",  5},
            {"ScoreA",  6},
            {"TimerSuspend",  7}
        };

        protected readonly Dictionary<string, int> GameEventIndexMapping = new Dictionary<string, int>()
        {
            {"MUID",  0},
            {"MatchId", 1},
            {"LeagueId", 4},
            {"LeagueName", 5},
            {"HomeName", 6},
            {"AwayName", 7},
            {"KickoffTime", 8},
            {"StatsId", 9},
            {"ShowTime", 12},
            {"HasLive", 13},
            {"TimerSuspend",15},
            {"FlagLive", 16},
            {"LivePeriod", 17},
            {"CsStatus", 18},
            {"InjuryTime", 19},
            {"ScoreH", 20},
            {"ScoreA",21},
            {"RedCardH", 22},
            {"RedCardA", 23}
        };

        protected readonly Dictionary<Odds.MarketType, int> GameEventOddsIndexMapping = new Dictionary<Odds.MarketType, int>()
        {
            {Odds.MarketType.G1X2, 35},
            {Odds.MarketType.FH1X2, 48},
            {Odds.MarketType.GHDP, 26},
            {Odds.MarketType.FHHDP, 39},
            {Odds.MarketType.GOU, 31},
            {Odds.MarketType.FHOU, 44}
        };

        protected readonly Dictionary<int,Odds.MarketType> GameEventOddsBetTypeMapping = new Dictionary<int,Odds.MarketType>()
        {
            {5, Odds.MarketType.G1X2},
            {15, Odds.MarketType.FH1X2},
            {1, Odds.MarketType.GHDP},
            {7, Odds.MarketType.FHHDP},
            {3, Odds.MarketType.GOU},
            {8, Odds.MarketType.FHOU}
        };

        protected readonly Dictionary<string, int> OddsHDPIndexMapping = new Dictionary<string, int>()
        {
            {"OddsId", 0},
            {"Handicap", 1},
            {"Home", 2},
            {"Away", 3},
            {"Focus", 4}
        };

        protected readonly Dictionary<string, int> Odds1X2IndexLiveMapping = new Dictionary<string, int>()
        {
             {"OddsId", 0},
             {"Home", 1},
             {"Away", 2},
             {"Tie", 3}
        };

        protected readonly Dictionary<string, int> Odds1X2IndexMapping = new Dictionary<string, int>()
        {
             {"OddsId", 0},
             {"Home", 1},
             {"Tie", 2}, 
             {"Away", 3}
        };


        protected readonly Dictionary<string, int> OddsOUIndexMapping = new Dictionary<string, int>()
        {
            {"OddsId", 0},
            {"Handicap", 1},
            {"Over", 2},
            {"Under", 3}
        };


        private Regex regNewEvents = new Regex(@"(?:Ne\[(?<i>\d+)\]=(?<j>\[[^\[\]]+\]);)", RegexOptions.Compiled);
        private Regex regLiveNewEvents = new Regex(@"(?:Nl\[(?<i>\d+)\]=(?<j>\[[^\[\]]+\]);)", RegexOptions.Compiled);
        private Regex regTodayNewEvents = new Regex(@"(?:Nt\[(?<i>\d+)\]=(?<j>\[[^\[\]]+\]);)", RegexOptions.Compiled);
        private Regex regLiveDell = new Regex(@"(?:Dell=(?<j>\[[^\[\]]+\]);)", RegexOptions.Compiled);
        private Regex regLiveInsl = new Regex(@"(?:Insl\[(?<i>\d+)\]=(?<j>\[[^\[\]]+\]);)", RegexOptions.Compiled);
        private Regex regLiveuLl = new Regex(@"(?:uLl\[(?<i>\d+)\]=(?<j>\[[^\[\]]+\]);)", RegexOptions.Compiled);
        private Regex regLiveuMl = new Regex(@"(?:uMl\[(?<i>\d+)\]=(?<j>\[[^\[\]]+\]);)", RegexOptions.Compiled);
        private Regex regLiveuAl = new Regex(@"(?:uAl\[(?<i>\d+)\]=(?<j>\[[^\[\]]+\]);)", RegexOptions.Compiled);
        private Regex regLiveuOl = new Regex(@"(?:uOl\[(?<i>\d+)\]=(?<j>\[[^\[\]]+\]);)", RegexOptions.Compiled);
        private Regex regNLiveDelt = new Regex(@"(?:Delt=(?<j>\[[^\[\]]+\]);)", RegexOptions.Compiled);
        private Regex regNLiveInst = new Regex(@"(?:Inst\[(?<i>\d+)\]=(?<j>\[[^\[\]]+\]);)", RegexOptions.Compiled);
        private Regex regNLiveuLt = new Regex(@"(?:uLt\[(?<i>\d+)\]=(?<j>\[[^\[\]]+\]);)", RegexOptions.Compiled);
        private Regex regNLiveuMt = new Regex(@"(?:uMt\[(?<i>\d+)\]=(?<j>\[[^\[\]]+\]);)", RegexOptions.Compiled);
        private Regex regNLiveuAt = new Regex(@"(?:uAt\[(?<i>\d+)\]=(?<j>\[[^\[\]]+\]);)", RegexOptions.Compiled);
        private Regex regNLiveuOt = new Regex(@"(?:uOt\[(?<i>\d+)\]=(?<j>\[[^\[\]]+\]);)", RegexOptions.Compiled);

        private Regex regValid = new Regex("window.location='(?<u>.*?)';", RegexOptions.Compiled);
        private Regex regShowTime = new Regex(@"(?:(?<p>\d)+H (?<m>\d+)')", RegexOptions.Compiled);
        private Regex regTickJson = new Regex(@"(?<=var\s*Data=)(?<j>.+)(?=;\s*parent\.SetBetProcessData)", RegexOptions.Singleline | RegexOptions.Compiled);
        private Regex regTickScore = new Regex(@"(?:\[(?<h>\d+)-(?<a>\d+)\])", RegexOptions.Compiled);
        private Regex regUpdateEventTime = new Regex(@"(?:parent\.ShowBetList\s*\(\s*'\w'\s*,\s*'(?<d>[^']+)'\s*,)", RegexOptions.Compiled);
        private Regex regBettingResultOrder = new Regex(@"var Data=(?<j>\{[^\{\}]+\});", RegexOptions.Compiled);
        private Regex regSocreMode1 = new Regex(@"\[(?<h>\d+)\-(?<v>\d+)\]", RegexOptions.Compiled);
        protected override string SessionID
        {
            get { return "ASP.NET_SessionId"; }
        }


        internal protected OddsDataFormParamters LiveFormParamters { get; set; }
        internal protected OddsDataFormParamters NoLiveFormParamters { get; set; }
        internal protected OddsDataFormParamters EarlyFormParamters { get; set; }
        internal protected Context FormRequestExtKey { get; set; }
        internal protected Context FormRequestExtValue { get; set; }

        protected DateTime? LastUpdateLiveOdds { get; set; }
        protected DateTime? LastUpdateNoLiveOdds { get; set; }
        protected DateTime? RequestTodayTime { get; set; }

        private int _todayTimeout = 600;
        /// <summary>
        /// 今日请求超时限制，单位（秒），默认600
        /// </summary>
        public int TodayTimeout
        {
            get { return _todayTimeout = 600; ; }
            set { _todayTimeout = value; }
        }


        protected virtual long GetUnixStampTicks()
        {
            var ticks= (long)(DateTime.Now.ToUniversalTime() - unixStampStart).TotalMilliseconds;
            return ticks;
        }
        private static DateTime unixStampStart = new DateTime(1970, 1, 1);
    }
}
